package cn.edu.hqu.cst.android.activitylifecycle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    final int MAIN_REQUEST_FOR_RESULT=0x01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.btn_main_activity_singleTask).setOnClickListener(this);
        findViewById(R.id.btn_main_activity_singleTop).setOnClickListener(this);
        findViewById(R.id.btn_main_activity_standard).setOnClickListener(this);
        findViewById(R.id.btn_main_activity_startActivityForResult).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent=new Intent();
        switch(v.getId()){
            case R.id.btn_main_activity_standard:
                intent.setClass(MainActivity.this, StandardActivity.class);
                break;
            case R.id.btn_main_activity_singleTop:
                intent.setClass(MainActivity.this, SingleTopActivity.class);
                break;
            case R.id.btn_main_activity_singleTask:
                intent.setClass(MainActivity.this, SingleTaskActivity.class);
                break;
            case R.id.btn_main_activity_startActivityForResult:
                intent.setClass(MainActivity.this, ReturnResultActivity.class);
                intent.putExtra("main_to_child","从MainActivity来的数据，除了字符串也可以是各类数据");
                startActivityForResult(intent,MAIN_REQUEST_FOR_RESULT);
                return;
        }
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Bundle bundle=data.getExtras();
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==MAIN_REQUEST_FOR_RESULT){
            //提取返回结果
            Toast.makeText(this,bundle.getString("result_to_main"),Toast.LENGTH_LONG).show();
        }
    }
}
